package com.plantpoint.controller;

import com.plantpoint.entity.vo.WaterVO;
import com.plantpoint.service.impl.PottedServiceImpl;
import com.plantpoint.entity.bean.CommonResult;
import com.plantpoint.entity.bean.PageResult;
import com.plantpoint.entity.dto.PottedAddDTO;
import com.plantpoint.entity.dto.PottedUpdateDTO;
import com.plantpoint.entity.pojo.PottedPlant;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author ATSELI
 * @version 1.0
 * @date 2021/4/20 18:02
 */

@RestController
@RequestMapping("/api/v1/user")
public class PottedController {

    private PottedServiceImpl pottedService;

    public PottedController(PottedServiceImpl pottedService) {
        this.pottedService = pottedService;
    }

    @PostMapping("/potted-info")
    public CommonResult<String> add(HttpServletRequest request, @Valid @RequestBody PottedAddDTO pottedPlant){
        int add = pottedService.add(request, pottedPlant);
        if (add == 1){
            return new CommonResult<>(20000, "OK", "添加成功");
        } else if (add == 0){
            return new CommonResult<>(50001, "ERROR", "添加失败");
        }else if (add == -2){
            return new CommonResult<>(20004, "OK", "该名称盆栽已存在");
        } else{
            return new CommonResult<>(40006, "ERROR", "词条不存在,请先进行添加词条");
        }
    }

    @GetMapping("/potted-info/{id}")
    public CommonResult<PottedPlant> query(@PathVariable String id){
        // System.out.println(id);
        PottedPlant plant = pottedService.query(id);
        if (plant == null){
            return new CommonResult<PottedPlant>(20004, "OK", null);
        }else {
            return new CommonResult<PottedPlant>(20000, "OK", plant);
        }
    }

    @PutMapping("/potted-info")
    public CommonResult<String> upd(@Valid @RequestBody PottedUpdateDTO pottedUpdateDTO){
        int result = pottedService.update(pottedUpdateDTO);
        if (result == 1){
            return new CommonResult<>(20000, "OK", "修改成功");
        } else {
            return new CommonResult<>(50001, "ERROR", "修改失败");
        }
    }


    @DeleteMapping("/potted-info/{id}")
    public CommonResult<String> del(@PathVariable String id){
        int result = pottedService.del(id);
        if (result == 1){
            return new CommonResult<>(20000, "OK", "删除成功");
        }else {
            return new CommonResult<>(50001, "ERROR", "删除失败");
        }
    }


    @GetMapping("/list/{cp}/{ps}")
    public CommonResult<PageResult<PottedPlant>> list(@PathVariable(value = "cp") Integer cp,
                                                      @PathVariable(value = "ps") Integer ps){
        PageResult<PottedPlant> plants = pottedService.list(cp, ps);
        return new CommonResult<>(20000, "OK", plants);
    }

    @PostMapping("/wat/{id}")
    public CommonResult<WaterVO> watering(@PathVariable(value = "id") String id){
        WaterVO times = pottedService.watering(id);
        return new CommonResult<>(20000, "OK", times);
    }
}
